package com.cabrida.livestar.mainstar;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ActionBar.LayoutParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.login.LoginActivity;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.mainstar.IndicatorMainStarView.OnIndicateListener;
import com.cabrida.livestar.model.FansCircleMsgChildrenData;
import com.cabrida.livestar.model.FansCircleMsgData;
import com.cabrida.livestar.model.FansGuardData;
import com.cabrida.livestar.model.FansStarData;
import com.cabrida.livestar.model.FansStarHomeData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.StarCircleData;
import com.cabrida.livestar.model.StarMainData;
import com.cabrida.livestar.util.Bimp;
import com.cabrida.livestar.util.DoubleClickExitHelper;
import com.cabrida.livestar.util.FileBitmapUtils;
import com.cabrida.livestar.util.FileUtils;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.ImageUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.util.SharedUtils;
import com.cabrida.livestar.util.StringUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 明星端
 */
public class MainStarActivity extends BaseActivity {
	public static final String TAG = MainStarActivity.class.getSimpleName();
	private DoubleClickExitHelper mDoubleClickExitHelper;
	private LinearLayout layout_body_main;
	private IndicatorMainStarView mIndicator;
	private StarHomePageView homePageView;
	private StarSpaceView spaceView;
	private StarMineView mineView;
	
	private Dialog dialogHead;

	private String protraitPath;
	private Bitmap protraitBitmap;
	private File protraitFile;
	private final static String FILE_SAVEPATH = FileBitmapUtils.SDPATH;
	private Uri origUri;
	private Uri cropUri;
	private final static int CROP = 600;

	/**
	 * 当前页面
	 */
	private int isFlag = IndicatorMainStarView.VIEW_HOMEPAGE;
	
	public static final String ACTION_MAINSTAR = "com.cabrida.livestar_mainstar";
	public static final String ACTION_MAINSTAR_ME = "com.cabrida.livestar_mainstar_me";
	public static final String ACTION_MAINSTAR_REFRESH = "com.cabrida.livestar_mainstar_refresh";
	public static final String ACTION_QUIT = "com.cabrida.livestar_quit";
	public static final String ACTION_CIRCLE_LIST = "com.cabrida.livestar_circlelist";
	public static final String ACTION_MESSAGE_LIST = "com.cabrida.livestar_msglist";
	public static final String ACTION_CIRCLELIKE_LIST = "com.cabrida.livestar_circlelist_like";
	public static final String ACTION_UPLOAD_PHOTO = "com.cabrida.livestar_upload_photo";
	public static final String ACTION_MESSAGE_BTN_CLICK = "com.cabrida.livestar_msg_btn_click";
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(ACTION_MAINSTAR)) {
				getMainStarList();
			}
			if (intent.getAction().equals(ACTION_MAINSTAR_ME)) {
				getStarData();
			}
			if (intent.getAction().equals(ACTION_MAINSTAR_REFRESH)) {
				getMainStarList();
			}
			if (intent.getAction().equals(ACTION_QUIT)) {
				QuitLogin();
			}
			if (intent.getAction().equals(ACTION_CIRCLE_LIST)) {
				getCircleList();
			}
			if (intent.getAction().equals(ACTION_MESSAGE_LIST)) {
				getMsgList();
			}
			if (intent.getAction().equals(ACTION_CIRCLELIKE_LIST)) {
				String cid  = intent.getStringExtra("cid");
				btnLike(cid);
			}
			if (intent.getAction().equals(ACTION_UPLOAD_PHOTO)) {
				uploadphoto();
			}
			if (intent.getAction().equals(ACTION_MESSAGE_BTN_CLICK)) {
				sendContent(intent.getStringExtra("description"),intent.getStringExtra("clickMsgCid"),intent.getStringExtra("clickMsgCudid"));
			}
		}
	};
	
	/** 发送回复 */
	private void sendContent(String description,String parentId,String pudid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", Global.U_id);
		params.put("type", Global.MESSAGE_TYPE_3);
		params.put("description", description);
		params.put("parent", parentId);
		params.put("pudid", pudid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CIRCLE_SEND_CONTENT_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "sendContent", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							getMsgList();
							hideSoftKeyboard();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 上传头像 */
	private void uploadphoto() {
		showHeadDialog((Activity) context);
	}
	
	private void showHeadDialog(Activity activity) {
		initHeadDialogView();
		if (null != dialogHead && dialogHead.isShowing()) {
			return;
		}
		if (null != dialogHead) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogHead.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) context).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogHead.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogHead.setCanceledOnTouchOutside(true);
			dialogHead.show();
		}
	}

	private void initHeadDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater.inflate(R.layout.dialog_img_choice, null);
		LinearLayout tvFriends = (LinearLayout) view
				.findViewById(R.id.ll_album);
		tvFriends.setOnClickListener(this);
		LinearLayout tvFriendsCircle = (LinearLayout) view
				.findViewById(R.id.ll_camera);
		tvFriendsCircle.setOnClickListener(this);
		Button btnCancle = (Button) view.findViewById(R.id.btn_cancel);
		btnCancle.setOnClickListener(this);
		dialogHead = new Dialog(context, R.style.dialogWindowStyle);
		dialogHead.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogHead.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	/**
	 * 选择图片裁剪
	 */
	private void startImagePick() {
		Intent intent;
		if (Build.VERSION.SDK_INT < 19) {
			intent = new Intent();
			intent.setAction(Intent.ACTION_GET_CONTENT);
			intent.setType("image/*");
			startActivityForResult(Intent.createChooser(intent, "选择图片"),
					ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP);
		} else {
			intent = new Intent(Intent.ACTION_PICK,
					Images.Media.EXTERNAL_CONTENT_URI);
			intent.setType("image/*");
			startActivityForResult(Intent.createChooser(intent, "选择图片"),
					ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP);
		}
	}

	/**
	 * 相机拍照
	 */
	private void startActionCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, this.getCameraTempFile());
		startActivityForResult(intent,
				ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA);
	}

	// 拍照保存的绝对路径
	private Uri getCameraTempFile() {
		String storageState = Environment.getExternalStorageState();
		if (storageState.equals(Environment.MEDIA_MOUNTED)) {
			File savedir = new File(FILE_SAVEPATH);
			if (!savedir.exists()) {
				savedir.mkdirs();
			}
		} else {
			showToast("无法保存上传的头像，请检查SD卡是否挂载");
			return null;
		}
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		// 照片命名
		String cropFileName = "osc_camera_" + timeStamp + ".jpg";
		// 裁剪头像的绝对路径
		protraitPath = FILE_SAVEPATH + cropFileName;
		protraitFile = new File(protraitPath);
		cropUri = Uri.fromFile(protraitFile);
		this.origUri = this.cropUri;
		return this.cropUri;
	}

	/**
	 * 拍照后裁剪
	 * 
	 * @param data
	 *            原始图片
	 */
	private void startActionCrop(Uri data) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(data, "image/*");
		intent.putExtra("output", this.getUploadTempFile(data));
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);// 裁剪框比例
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", CROP);// 输出图片大小
		intent.putExtra("outputY", CROP);
		intent.putExtra("scale", true);// 去黑边
		intent.putExtra("scaleUpIfNeeded", true);// 去黑边
		startActivityForResult(intent,
				ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD);
	}

	// 裁剪头像的绝对路径
	private Uri getUploadTempFile(Uri uri) {
		String storageState = Environment.getExternalStorageState();
		if (storageState.equals(Environment.MEDIA_MOUNTED)) {
			File savedir = new File(FILE_SAVEPATH);
			if (!savedir.exists()) {
				savedir.mkdirs();
			}
		} else {
			showToast("无法保存上传的头像，请检查SD卡是否挂载");
			return null;
		}
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		String thePath = ImageUtils.getAbsolutePathFromNoStandardUri(uri);
		// 如果是标准Uri
		if (StringUtils.isEmpty(thePath)) {
			thePath = ImageUtils.getAbsoluteImagePath(this, uri);
		}
		String ext = FileUtils.getFileFormat(thePath);
		ext = StringUtils.isEmpty(ext) ? "jpg" : ext;
		// 照片命名
		String cropFileName = "osc_crop_" + timeStamp + "." + ext;
		// 裁剪头像的绝对路径
		protraitPath = FILE_SAVEPATH + cropFileName;
		protraitFile = new File(protraitPath);
		cropUri = Uri.fromFile(protraitFile);
		return this.cropUri;
	}
	
	/**
	 * 上传新照片
	 */
	private void uploadNewPhoto() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		try {
			params.put("img", protraitFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_IMGUPLOAD_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "uploadNewPhoto", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String str_path = JsonUtils.getString(data,"path", "");
							String str_file = JsonUtils.getString(data,"file", "");
							protraitBitmap = Bimp.getSmallBitmap(protraitPath);
							mineView.setUploadPhoto(protraitBitmap);
							upBackDropData(str_path, str_file);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void onActivityResult(final int requestCode,
			final int resultCode, final Intent data) {
		if (resultCode != RESULT_OK)
			return;
		switch (requestCode) {
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA:
			startActionCrop(origUri);// 拍照后裁剪
			break;
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP:
			startActionCrop(data.getData());// 选图后裁剪
			break;
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD:
			showProgress();
			uploadNewPhoto();// 上传头像
			break;
		}
	}
	
	/** 上传明星背景 */
	private void upBackDropData(String str_path,String str_file) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("path", str_path);
		params.put("file", str_file);

		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_MAIN_STAR_UP_BACKDROP_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "upBackDropData", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);

						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message = JsonUtils.getString(data, "message", "");
							showToast(message);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取留言数据 */
	private void getMsgList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", Global.U_id);
		params.put("type", Global.MESSAGE_TYPE_3);
		params.put("page", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARCIRCLEMSG_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getMsgList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							ArrayList<FansCircleMsgData> infoList = JSONParser.getFansCircleMsgList(list);
							spaceView.putMsgData(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取星友圈数据 */
	private void getCircleList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARCIRCLELIST_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getCircleList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							ArrayList<StarCircleData> infoList = JSONParser.getFansCircleList(list);
							spaceView.putData(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 点赞 */
	private void btnLike(final String cid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", cid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CIRCLE_LIKE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "btnLike", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							String number = JsonUtils.getString(data,"number", "");
							spaceView.putLikeData(cid, number);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	private void QuitLogin(){
		clearData();
		this.finish();
		openActivity(LoginActivity.class);
	}
	
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_main_star);

		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

		Global.Screen_width = displaymetrics.widthPixels;
		Global.Screen_height = displaymetrics.heightPixels;

		layout_body_main = (LinearLayout) findViewById(R.id.layout_body_main);
		
		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(ACTION_MAINSTAR);
		filter_ad.addAction(ACTION_MAINSTAR_ME);
		filter_ad.addAction(ACTION_MAINSTAR_REFRESH);
		filter_ad.addAction(ACTION_QUIT);
		filter_ad.addAction(ACTION_CIRCLE_LIST);
		filter_ad.addAction(ACTION_CIRCLELIKE_LIST);
		filter_ad.addAction(ACTION_UPLOAD_PHOTO);
		filter_ad.addAction(ACTION_MESSAGE_LIST);
		filter_ad.addAction(ACTION_MESSAGE_BTN_CLICK);
		registerReceiver(mReceiver, filter_ad);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mDoubleClickExitHelper = new DoubleClickExitHelper(this);
		mIndicator = (IndicatorMainStarView) findViewById(R.id.indicator);
		addViewBody(isFlag);
		mIndicator.setIndicator(isFlag);
		mIndicator.setOnIndicateListener(new OnIndicateListener() {
			@Override
			public void onIndicate(View v, int which) {
				addViewBody(which);
			}
		});
	}
	
	/** 获取明星主页数据 */
	private void getStarData() {
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("suid", Global.U_id);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_MAIN_STAR_STAR_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAttentionList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONObject star = JsonUtils.getJSONObject(data, "star", new JSONObject());
							FansStarData stardata = new FansStarData();
							stardata.setU_avatar(JsonUtils.getString(star,"u_avatar", ""));
							stardata.setU_obtain(JsonUtils.getString(star,"u_obtain", ""));
							stardata.setS_udid(JsonUtils.getString(star,"s_udid", ""));
							stardata.setS_name(JsonUtils.getString(star,"s_name", ""));
							stardata.setS_backdrop(JsonUtils.getString(star,"s_backdrop", ""));
							stardata.setS_booked(JsonUtils.getString(star,"s_booked", ""));
							stardata.setS_musictype(JsonUtils.getString(star,"s_musictype", ""));
							
							FansStarHomeData starhomedata = new FansStarHomeData();
							starhomedata.setStarData(stardata);
							starhomedata.setLive(JsonUtils.getString(data,"live", ""));
							starhomedata.setAdvance(JsonUtils.getString(data,"advance", ""));
							starhomedata.setPlayBack(JsonUtils.getString(data,"playBack", ""));
							starhomedata.setCircle(JsonUtils.getString(data,"circle", ""));
							
							
							JSONObject guard = JsonUtils.getJSONObject(data, "guard", new JSONObject());
							starhomedata.setGuardNum(JsonUtils.getString(guard,"num", ""));
							JSONArray list = JsonUtils.getJSONArray(guard, "list", new JSONArray());
							ArrayList<FansGuardData> infoList = JSONParser.getFansGuardList(list);
							starhomedata.setList(infoList);
							mineView.setData(starhomedata);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 获取列表 */
	private void getMainStarList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_MAIN_STAR_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "main_star_response", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							ArrayList<StarMainData> infoList = JSONParser.getStarMainList(list);
							homePageView.notifyStarData(infoList);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	/**
	 * 动态添加布局
	 */
	private void addViewBody(int flag) {
		isFlag = flag;
		switch (flag) {
		case IndicatorMainStarView.VIEW_HOMEPAGE:
			if (null == homePageView) {
				homePageView = new StarHomePageView(context);
			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(homePageView);
			break;

		case IndicatorMainStarView.VIEW_DYNAMIC:
			if (null == spaceView) {
				spaceView = new StarSpaceView(context);
			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(spaceView);
			break;
			
		case IndicatorMainStarView.VIEW_MINE:
			if (null == mineView) {
				mineView = new StarMineView(context);
			}
			layout_body_main.removeAllViews();
			layout_body_main.addView(mineView);
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.ll_album:
			if (null != dialogHead) {
				dialogHead.dismiss();
				startImagePick();
			}
			break;
		case R.id.ll_camera:
			if (null != dialogHead) {
				dialogHead.dismiss();
				startActionCamera();
			}
			break;
		case R.id.btn_cancel:
			if (null != dialogHead) {
				dialogHead.dismiss();
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 监听返回--是否退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 是否退出应用
			return mDoubleClickExitHelper.onKeyDown(keyCode, event);
		} else {
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}
}
